import * as VueRouter from 'vue-router'
import { jwtDecode } from 'jwt-decode'

const routes = [

    {
        path: '/',
        component: () => import('../layouts/index.vue'),
        redirect: '/platform-user',
        children: [
            // {
            //     name: 'home',
            //     path: '/home',
            //     component: () => import('../views/home/index.vue'),
            //     meta: { requiresAuth: true }
            // },
            {
                name: 'platform-user',
                path: '/platform-user',
                component: () => import('../views/platform-user/index.vue'),
                meta: { requiresAuth: true }
            },

            {
                name: 'platform-active-ball',
                path: '/platform-active-ball',
                component: () => import('../views/platform-active-ball/index.vue'),
                meta: { requiresAuth: true, title: '活动管理' }
            },
            {
                name: 'platform-user-suggest',
                path: '/platform-user-suggest',
                component: () => import('../views/platform-user-suggest/index.vue'),
                meta: { requiresAuth: true, title: '用户建议' }
            },
            {
                name: 'platform-match-record',
                path: '/platform-match-record',
                component: () => import('../views/platform-match-record/index.vue'),
                meta: { requiresAuth: true, title: '比赛记录' }
            },

            {
                name: 'knowledge',
                redirect: '/platform-motion',
                path: 'platform-motion',
                children: [
                    // 动作管理
                    { name: 'platform-motion', path: '/platform-motion', component: () => import('../views/platform-motion/index.vue') },
                    // 动作目录管理
                    { name: 'platform-motion-catalog', path: '/platform-motion-catalog', component: () => import('../views/platform-motion-catalog/index.vue') },
                    // 动作素材审核
                    { name: 'platform-motion-material', path: '/platform-motion-material', component: () => import('../views/platform-motion-material/index.vue') },
                ],
                meta: { requiresAuth: true, title: '涨球管理' }
            },

            {
                name: 'score',
                redirect: '/platform-point-rule-s',
                path: '/score',
                children: [
                    { name: 'platform-points-rule-s', path: '/platform-points-rule-s', component: () => import('../views/platform-points-rule-s/index.vue') },
                ],
                meta: { requiresAuth: true, title: '比赛管理' }
            },

            {
                name: 'points',
                redirect: '/platform-point',
                path: '/points',
                children: [
                    { name: 'platform-points', path: '/platform-points', component: () => import('../views/platform-points/index.vue') },
                    { name: 'platform-points-rule-p', path: '/platform-points-rule-p', component: () => import('../views/platform-points-rule-p/index.vue') },
                ],
                meta: { requiresAuth: true, title: '积分管理' }
            },

            {
                name: 'article',
                path: '/article',
                redirect: '/platform-article-list',
                children: [
                    { name: 'platform-article-list', path: '/platform-article-list', component: () => import('../views/platform-article/index.vue'), },
                    // { name: 'platform-article-classify', path: '/platform-article-classify', component: () => import('../views/platform-article/article-classify.vue') },
                ],

                meta: { requiresAuth: true, title: '文章管理' }
            },

            {
                name: 'wechat',
                path: '/subscribe-template',
                redirect: '/subscribe-template',
                children: [
                    { name: 'subscribe-template', path: '/subscribe-template', component: () => import('../views/wechat-subscribe-template/index.vue') },
                ],
                meta: { requiresAuth: true, title: '小程序订阅管理' }
            },

            {
                name: 'sys',
                redirect: '/sys-user',
                path: 'manage-permission',
                children: [
                    { name: 'sys-user', path: '/sys-user', component: () => import('../views/sys-user/index.vue') },
                    { name: 'sys-menu', path: '/sys-menu', component: () => import('../views/sys-menu/index.vue') },
                    { name: 'sys-role', path: '/sys-role', component: () => import('../views/sys-role/index.vue') },
                ],
                meta: { requiresAuth: true }
            },
        ]
    },

    {
        name: 'login', path: '/login', component: () => import('../views/login/index.vue')
    },

    { name: 'mobile-preview', path: '/mobile-preview', component: () => import('../views/platform-article/mobile-preview.vue')},
]
const router = VueRouter.createRouter({
    history: VueRouter.createWebHashHistory(),
    routes: routes,
})

// router.beforeEach((to, from, next) => {
//     const token = localStorage.getItem('token')
//
//     if (to.matched.some(record => record.meta.requiresAuth)) {
//         if (token && isTokenValid(token)) {
//             next()
//         } else {
//             next({
//                 path: '/login',
//                 query: { redirect: to.fullPath }
//             })
//         }
//     } else {
//         next() // 确保一定要调用 next()
//     }
// })

export default router

/**
 * 判断token是否过期
 * @param token
 * @returns {boolean}
 */
function isTokenValid (token) {
    if (!token) {
        return false
    }
    try {
        const decodedJwt = jwtDecode(token)
        const currentTime = Date.now() / 1000

        if (typeof decodedJwt.exp === 'undefined') {
            return false
        }
        return decodedJwt.exp > currentTime
    } catch (err) {
        return false
    }
}


